package com.controller;


import com.bean.ResponseBean;
import com.util.MinioUtil;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.net.URLEncoder;

//@Api(tags = "文件操作接口")
@RestController
public class FileController {
	
	@Autowired
	MinioUtil minioUtil;
	@Value("${minio.bucketName}")
	private String bucket ;
//	@ApiOperation("上传一个文件")
	@RequestMapping(value = "/uploadfile", method = RequestMethod.POST)
	public ResponseBean fileupload(@RequestParam MultipartFile file,
								   @RequestParam(required=false) String objectName) throws Exception {
//		System.out.println(bucket);
//		minioUtil.createBucket(bucket);
		if (objectName != null) {
			minioUtil.uploadFile(file.getInputStream(), bucket, objectName+"/"+file.getOriginalFilename());
		} else {
			minioUtil.uploadFile(file.getInputStream(), bucket, file.getOriginalFilename());
		}
		return ResponseBean.OK;
	}
	
//	@ApiOperation("列出所有的桶")
	@RequestMapping(value = "/listBuckets", method = RequestMethod.GET)
	public ResponseBean listBuckets() throws Exception {
		return new  ResponseBean(200,"查询成功",minioUtil.listBuckets());
	}
	
//	@ApiOperation("递归列出一个桶中的所有文件和目录")
	@RequestMapping(value = "/listFiles", method = RequestMethod.GET)
	public ResponseBean listFiles(@RequestParam String bucket) throws Exception {
		return new ResponseBean(200, "查询成功",minioUtil.listFiles(bucket));
	}
	
//	@ApiOperation("下载一个文件")
	@RequestMapping(value = "/downloadFile", method = RequestMethod.GET)
	public void downloadFile(@RequestParam String bucket, @RequestParam String objectName,
                             HttpServletResponse response) throws Exception {
		InputStream stream = minioUtil.download(bucket, objectName);
		ServletOutputStream output = response.getOutputStream();
		response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(objectName.substring(objectName.lastIndexOf("/") + 1), "UTF-8"));
        response.setContentType("application/octet-stream");
        response.setCharacterEncoding("UTF-8");
		IOUtils.copy(stream, output);
	}
	
	
//	@ApiOperation("删除一个文件")
	@RequestMapping(value = "/deleteFile", method = RequestMethod.GET)
	public ResponseBean deleteFile(@RequestParam String bucket, @RequestParam String objectName) throws Exception {
		minioUtil.deleteObject(bucket, objectName);
		return ResponseBean.OK;
	}
	
//	@ApiOperation("删除一个桶")
	@RequestMapping(value = "/deleteBucket", method = RequestMethod.GET)
	public ResponseBean deleteBucket(@RequestParam String bucket) throws Exception {
		minioUtil.deleteBucket(bucket);
		return ResponseBean.OK;
	}	
}